<?php
setlocale(LC_NUMERIC, 'en_US');
define ('SQLITE_DATABASE', "track.db");

if (!file_exists(SQLITE_DATABASE))
{
	// create new database using the OOP approach

	$db = new SQLiteDatabase(SQLITE_DATABASE, 0666, $error);
	if (!$db) 
	{
		header("HTTP/1.0 503 Database Unavailable");
		printf("Impossible to create database: %s\n", $error);
		exit();
	}	// create tables
	
	$db->query("
		BEGIN;
		CREATE TABLE objects (
			id INTEGER(4) PRIMARY KEY,
			Key CHAR(255) UNIQUE,
			Name CHAR(255), 
			PermURL CHAR(255), 
			Location CHAR(255), 
			OwnerKey CHAR(36),
			OwnerName CHAR(255),
			LastUpdate CHAR(255)
		);
		CREATE TABLE avatars (
			id INTEGER(4) PRIMARY KEY,
			Key CHAR(255),
			Name CHAR(255),
			ObjectKey CHAR(255),
			ObjectName CHAR(255),
			Location CHAR(255), 
			Pos CHAR(255), 
			Rot CHAR(255), 
			Vel CHAR(255), 
			LastUpdate CHAR(255)
		);
		COMMIT;"
	);
}
else
{
	$db = new SQLiteDatabase(SQLITE_DATABASE, 0666, $error);

	if (!$db)
	{
		header("HTTP/1.0 503 Service Unavailable");
		printf("Connect failed: %s\n", $error);
		exit();
	}
}
?>